<?php
$page = 'admin';
include '../database/connect.php';
$quiz_id = $_GET['quiz_id'];
if (isset($_GET['save'])) {
    session_start();
    extract($_POST);
    $added_by = $_SESSION['username'];
    $query = "INSERT INTO questions SET quiz_id='$quiz_id', content='$content',marks='$marks',  added_by='$added_by'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?quiz_id=$quiz_id");
}


$query = "select * from quizes where quiz_id='$quiz_id'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
$row = mysql_fetch_assoc($result);
$course_id = $row['course_id'];
$quiz= $row['description'];

//lets get the course name for purpose of navigation
$query = "SELECT * FROM courses where id='$course_id'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
$row = mysql_fetch_assoc($result);
$course = $row['course'];

require_once 'header.php'; ?>
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#quiz_id_form").validationEngine();
    });
</script>



<p>
    <a href="admin_home.php">Admin home</a>&gt;&gt;<a href='courses.php'>Courses</a>&gt;&gt;
    <a href="lessons.php?course_id=<?php echo $course_id; ?>"><?php echo $course; ?></a>&gt;&gt;
    <?php echo $quiz; ?>
</p>

<form action="?save=1" method="POST" id="questions_form">
    <input type="hidden" name="quiz_id" value="<?php echo $_GET['quiz_id']; ?>" />
    <table class="table">
        <tr style="text-align: left"><th>Question</th><th>Marks</th></tr>
        <tr>
            <td><textarea style="width: 90%" class="validate[required] wide" name="content" id='content'></textarea></td>
            <td><input type="text" name="marks" size="2" /> </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: right;"><input type="submit" value="Add Question"/></td>
        </tr>
    </table>
</form>
<?php
$username = $_SESSION['username'];
$query = "SELECT * FROM questions where quiz_id='$quiz_id' order by date_added asc";
$count = 0;
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
if (mysql_num_rows($result) == 0) {
//    echo "No in the database";
} else {
?>

    <table class="table" style="text-align: left">
        <tr>
            <th >

            </th>
            <th >
                Question
            </th>
            <th >
                Marks
            </th>
    <!--            <th >
                Edit
            </th>

            <th >Delete
            </th>-->
        </tr>
    <?php
    while ($row = mysql_fetch_assoc($result)) {
        //var_dump($row);
        extract($row);
        $count++;
    ?>
        <tr>
            <td >
            <?php echo $count; ?>
        </td>
        <td >
            <a href="answers.php?question_id=<?php echo $question_id; ?>"><?php echo $content; ?></a>
        </td>
        <td >
            <?php echo $marks; ?>
        </td>

<!--                <td > 
                    <a href="javascript:editquiz_id(<?php echo $id ?>);"><img src="../images//edit.png" alt="Edit" title="Edit"/></a>
                </td><td>
                    <a href="javascript:deletequiz_id(<?php echo $id ?>);"><img src="../images//delete.png" alt="Delete" title="Delete"/></a>
                </td>-->
    </tr>
    <?php
        }
    }
    ?>
</table>


<?php require_once('footer.php'); ?>